<?php
require_once ('odal/meta/namespace.php');
require_once ('cube/core/cache/cache.php');
/**
 * @version 1.0
 * @created 27-Dez-2003 21:35:24
 */
class odal_OMSMetaData{
	
	var $m_schemes = Array();
	var $m_namespaces = Array();
	var $m_namespacesPath = Array();
	var $m_complexAttributes = Array();
	var $m_omsData;
	var $m_cache;
	var $m_useCache = true;
	
	function odal_OMSMetaData( &$omsData ){
		$this->m_omsData =& $omsData;
		$this->_setCache();
	}//odal_OMSMetaData
	
	function _useCache(){
		return $this->m_useCache;
	}//_useCache

	function appendNamespace( &$namespace ){
		$this->m_namespaces[$namespace->getId()] =& $namespace;
		$this->m_namespacesPath[$namespace->getPath()] =& $namespace;
		//echo "[omsmetadata->appendNamespace] namespace->getId(): " . $namespace->getId() . "<br>";
	}//appendNamespace 

	function appendScheme( &$scheme ){
		$this->m_schemes[$scheme->getId()] =& $scheme;
		//echo "[omsmetadata->appendSchemes] scheme->getId(): " . $scheme->getId() . "<br>";
	}//appendScheme 
	
	function appendComplexAttribute( &$complexAttribute ){
		$this->m_complexAttributes[$complexAttribute->getId()] =& $complexAttribute;
	}//appendScheme 

	function &createMetaDataBySchemeId( $scheme_id, $useCache = false ){
		//$useCache = true;
		$oScheme = null;
		if ( $this->_useCache() ){
			$oScheme =& $this->_getMetaDataFromCache( $this->_getCacheKey( "scm_" . $scheme_id ) );
			if ( $oScheme != null ){
				$this->appendScheme( $oScheme );
				//echo "[omsmetadata->createMetaDataBySchemeId] got scheme_id: '" . $scheme_id . "' from cache <br>\n";
//				echo "[omsmetadata->createMetaDataBySchemeId] oScheme->getNamespaceId(): '" . $oScheme->getNamespaceId(). "' <br>\n";
				//$oNamespace =& $this->m_omsData->_setNamespaceMetaDataFromDB( $oScheme->getNamespaceId() );
				$oNamespace =& $this->createMetaDataByNamespaceId( $oScheme->getNamespaceId() );
				$oScheme->setNamespace( $oNamespace );
				$oNamespace->appendScheme( $oScheme->getId(), $oScheme );
				foreach( $oScheme->m_complexAttributes as $id => $dummy ){
					$oCat =& $oScheme->m_complexAttributes[$id];
					$this->appendComplexAttribute( $oCat );
					unset( $oCat );
				}//foreach
			}//if
		}//if
		if ( $oScheme == null ){
			//echo "[omsmetadata->createMetaDataBySchemeId] have to get scheme_id: '" . $scheme_id . "' from db <br>\n";
			$oScheme =& $this->m_omsData->_pck_setSchemeMetaDataFromDB( $scheme_id );
			//echo "[omsmetadata->createMetaDataBySchemeId] oScheme->getId(): " . $oScheme->getId() . "<br>";
			$this->appendScheme( $oScheme );
		}//if
		//print_r( $oScheme );
		return $oScheme;
	}//createMetaDataBySchemeId

	function &_createMetaDataByNamespace( $nsp_key, $type ){
		//$useCache = true;
		$oNamespace = null;
		$oScheme = null;
		if ( $this->_useCache() ){
			$oNamespace =& $this->_getMetaDataFromCache( $this->_getCacheKey( "nsp_" . $nsp_key ) );
			if ( $oNamespace != null ){
				//echo "[omsmetadata->createMetaDataByNamespaceId] got nsp_id: '" . $nsp_id . "' from cache <br>\n";
			}//if
		}//if
		if ( $oNamespace == null ){
			$oNamespace =& $this->m_omsData->_setNamespaceMetaDataFromDB( $nsp_key, $type );
		}//if
		$this->appendNamespace( $oNamespace );
		return $oNamespace;
	}//createMetaDataByNamespaceId
	
	function &createMetaDataByNamespacePath( $nsp_path ){
		$nsp_path ="'".$nsp_path."'";
		return $this->_createMetaDataByNamespace( $nsp_path, 'path' );
	}//createMetaDataByNamespacePath

	function &createMetaDataByNamespaceId( $nsp_id ){
		return $this->_createMetaDataByNamespace( $nsp_id, 'id' );
	}//createMetaDataByNamespaceId
	
	function &getComplexAttributeById( $id ){
//		echo "[omsmetadata->getComplexAttributeById] id: $id <br>";
		$oCat =& $this->_getMetaObjectById( $id, $this->m_complexAttributes , "getComplexAttributeById");
		return $oCat;
	}//getSchemeById

	function &getNamespaceById( $id ){
		$oNsp =& $this->_getMetaObjectById( $id, $this->m_namespaces , "getNamespaceById");
		if ( $oNsp == null ){ 
			$this->createMetaDataByNamespaceId( $id, true);
		}//if
		return $this->_getMetaObjectById( $id, $this->m_namespaces , "getNamespaceById");
	}//getNamespaceById

	function &getNamespaceByPath( $path ){
		$oNsp =& $this->_getMetaObjectById( $path, $this->m_namespacesPath , "getNamespaceByPath");
		if ( $oNsp == null ){ 
			$this->createMetaDataByNamespacePath( $path, true );
		}//if
		return $this->_getMetaObjectById( $path, $this->m_namespacesPath , "getNamespaceByPath");
	}//getNamespaceByPath

	function &getNamespaceMetaData( $nsp_id, &$omsData, $useCache = false ){
		$oNamespace = null;
		if ( $useCache ){
			$key = "nsp_" . $nsp_id;
			$oNamespace =& $this->_getMetaDataFromCache( $this->_getCacheKey( $key ) );
		}//if
		if ( $oNamespace == null ){
			$oNamespace =& $omsData->getNamespaceMetaDataFromDB( $nsp_id );
			//echo "(omsmetadata->getNamespaceMetaData()) oNamespace->getId(): " . $oNamespace->getId() . "<br>";  
			if ( $useCache && ( $oNamespace != null ) ){
				$this->writeToCache( $oNamespace, $key );
			}//if
		}//if
		if ( $oNamespace != null ){
			$this->appendNamespace( $oNamespace );
			//$this->m_namespaces[$oNamespace->getId()] =& $oNamespace;
		}//if
		return $oNamespace;
	}//getNamespaceMetaData
	
	function &getSchemeById( $id, $loadLazy=false ){
		$oScheme=& $this->_getMetaObjectById( $id, $this->m_schemes ,"getSchemeById");
		if ( $oScheme == null &&  $loadLazy==true) {
			$this->createMetaDataBySchemeId( $id );
			$oScheme=& $this->_getMetaObjectById( $id, $this->m_schemes, "getSchemeById");
		}//if
		return $oScheme;
	}//getSchemeById
	

	function &getScheme( $namespacePath, $schemeName ){
		//echo "[omsmetadata->getScheme] namespacePath: " . $namespacePath . " - schemeName: " . $schemeName . "<br>\n";
		$retscm = null;
		foreach ( $this->m_schemes as $id => $scheme ){
			if ( $scheme->getName() == $schemeName ){
				//echo "[omsmetadata->getScheme] scheme->getName(): " . $scheme->getName() . "<br>";
				$namespace = $scheme->getNamespace();
				if ( ( $scheme->getName() == $schemeName ) && ( $namespace->getPath() == $namespacePath ) ){
					$retscm =& $this->getSchemeById( $id );
					break; 
				}//if  
			}//if  
		}//foreach
		if( $retscm == null){
			$key = $this->_getCacheKey( $namespacePath . '/' . $schemeName );
			$schemeId = $this->_getMetaDataFromCache( $this->_getCacheKey( $key ) );
			if ( $schemeId == null ){
				$aSqlBindvars['schemeId'] = null;
				$aSqlParams['scm_name'] = $schemeName;
				$aSqlParams['nsp_path'] = $namespacePath;
				$this->m_omsData->executeOnResource( "getschemeidbyscmnameandnsppath", $aSqlParams, $aSqlBindvars );
				//echo "[omsmetadata->getScheme] aSqlBindvars['schemeId']: " . $aSqlBindvars['schemeId'] . "<br>";
				$schemeId = $aSqlBindvars['schemeId'];
				$this->writeToCache( $schemeId, $key );
			}//if
			$this->createMetaDataBySchemeId( $schemeId, false );
			$retscm =& $this->getSchemeById( $schemeId );
			//echo "[omsmetadata->getScheme] retscm->getName(): " . $retscm->getName() . "<br>";
		}//if
		return $retscm;
	}//getScheme

	function &_getMetaObjectById( $id, &$objColl , $method = "" ){
		$retobj = null;
		if ( isset( $objColl[$id] ) ){
			$retobj =& $objColl[$id];
		}//if
		return $retobj;
	}//_getMetaObjectById
		
	function &_getMetaObjectByName( $name, &$objColl ){
		$retobj = null;
		foreach ( $objColl as $obj ){
			//echo "(omsmetadata->_getMetaObjectByName) obj->getName(): " . $obj->getName() . "<br>";
			if ( $obj->getName() == $name ){
				$retobj = $obj;
			}//if
		}//foreach
		return $retobj;
	}//_getMetaObjectByName

	function _getCache(){
		return $this->m_cache;
	}//_getCache
	
	function &_getMetaDataFromCache( $key ){
		$oMeta = null;
		if ( isset( $this->m_aCacheCache[ $key ] ) ){
			$oMeta =& $this->m_aCacheCache[ $key ];
		} else {
			$cache = $this->_getCache();
			if ( $cache->existInCache( $key ) ){
				$cache_item = $cache->getFromCache( $key );
				//echo $cache_item->getPath();
				$oMeta = unserialize( implode( '', file( $cache_item->getPath() ) ) );
				$this->m_aCacheCache[ $key ] =& $oMeta;
			}//if
		}//else
		return $oMeta;		
	}//_getMetaDataFromCache
	
	function _getCacheKey( $p_key ){
//		echo "[omsmetadata->_getCacheKey] connectionAlias: " . $this->m_omsData->getConnectionAlias() . "<br>\n";
		return $this->m_omsData->getConnectionAlias() . $p_key;
	}//_getCacheKey
	
	function _setCache(){
		$this->m_cache =& new cache_cacheManager( 'odal.metaData' );
		$this->m_cache->setUseAbsoluteCachePath( true );
		//$this->m_cache->setCacheRootFolder( "/media/tmp/metacache" ); 
	}//_setCache

	function writeToCache( $item, $key ){
		$cache = $this->_getCache();
		//echo '[omsmetadata->writetocache] print_r( $item ): ' . print_r( $item ) . "<br>\n"; 
		$cache->writeToCache( new cache_objectWriter( $item ), $this->_getCacheKey( $key ) );
	}//writeToCache
	
	function __TODO__(){}
	
	function getMetaData(){
	}

	function loadMetaData(){
	}
}//odal_OMSMetaData
?>